/**
 * 15/02/2014
 * 
 * StateController
 */
package org.cuatrovientos.erp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.cuatrovientos.erp.business.ProductTypeBOInterface;
import org.cuatrovientos.erp.model.ProductType;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Controller class form ProductType management
 * @author Inaki Torres
 */
@Controller
public class ProductTypeController {

	//Get log4j logger
	private static final Logger logger = 
			Logger.getLogger(ProductTypeController.class);

	@Autowired
	private ProductTypeBOInterface productTypeBO;

	/**
	 * default constructor
	 */
	public ProductTypeController () {
		logger.info("ProductType BEAN instantiated");
	}

	/**
	 * handles default /productType or /productType/list request
	 * @return
	 */
	@RequestMapping({"/productType","/productType/list"})
	public ModelAndView productTypeDefault() {
		logger.info("default");

		Map<String, Object> model = new HashMap<String, Object>();

		List<ProductType> productTypeList = productTypeBO.selectAll();
		model.put("productTypes", productTypeList );
		
		return new ModelAndView("ProductTypeList", model);     
	}


	/**
	 * handles default /productType/search request
	 * @param name
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST,value={"/productType/search"})
	public ModelAndView search(
			@RequestParam(value="name", defaultValue="", required=true) 
			String description) {
		
		logger.info("search");

		Map<String, Object> model = new HashMap<String, Object>();

		List<ProductType> productTypeList = productTypeBO.search(description);
		model.put("productTypes", productTypeList );
		
		return new ModelAndView("ProductTypeList", model);     
	}

	/**
	 * handles default /productType/delete request
	 * @param id
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/productType/delete"})
	public String deleteProductType(@RequestParam(value="id", defaultValue="", required=true) int id) {
		logger.info("delete " + id);

		productTypeBO.deleteById(id);
		
		return "redirect:/productType";
	}

	/**
	 * handles default /productType/new request
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/productType/new"})
	public ModelAndView newProductType() {
		logger.info("new productType");
		
		ProductType productType = new ProductType();

		return new ModelAndView("NewProductType", "productType", productType);
	}

	/**
	 * handles default /productType/save request
	 * @param productType
	 * @param bindingResult
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST,value={"/productType/save"})
	public String newProductType(@Valid ProductType productType, BindingResult bindingResult) {
		logger.info("new productType " + productType.toString());

		//In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("productType", productType);
			
			return "NewProductType";
		}

		productTypeBO.insert(productType);
		
		return "redirect:/productType";
	}

	/**
	 * handles default /productType/detail request
	 * @param id
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/productType/detail"})
	public ModelAndView productTypeDetail(@RequestParam(value="id", defaultValue="", required=true) int id) {
		logger.info("Detail " + id);
		
		Map<String,Object> model = new HashMap<String,Object>();
		ProductType productType = productTypeBO.selectById(id);
		model.put("role", productType);

		return new ModelAndView("ProductTypeDetail", model);
	}


	/**
	 * handles default /productType/update request
	 * @param id
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/productType/update"})
	public ModelAndView update(@RequestParam(value="id", defaultValue="", required=true) int id) {
		logger.info("update " + id);
		
		Map<String,Object> model = new HashMap<String,Object>();
		ProductType productType = productTypeBO.selectById(id);
		model.put("role", productType);
		
		return new ModelAndView("ProductTypeRole",model);
	}


	/**
	 * handles default /productType/saveupdate request
	 * @param productType
	 * @param bindingResult
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST,value={"/productType/saveupdate"})
	public String saveUpdate(@Valid ProductType productType, BindingResult bindingResult) {
		logger.info("update productType " + productType.toString());

		// In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("productType", productType);
			
			return "UpdateProductType";
		}

		productTypeBO.update(productType);
		
		return "redirect:/productType";
	}
	
	/**
	 * @param model
	 * @return
	 */
	@RequestMapping({"/producttype/index"})
	public String sayHelloPage(Map<String, Object> model) {
		return "index/IndexIssuetype";
	}
	
}
